package com.android.view.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.android.util.WindowUtils;
import com.android.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by TenFace
 * on 2018/4/19.
 */

public class CommonHeaderView extends FrameLayout {
    private static final String TAG = "CommonHeaderView";

    @OnClick(R.id.custom_header_iv_back)
    void clickBack() {
        if (openCloseBtn) {
            if (backClickCallback != null) {
                backClickCallback.back(ivClose);
            }
        } else {
            if (backClickCallback != null) {
                backClickCallback.back(ivBack);
            } else {
                ((Activity) getContext()).finish();
            }
        }
    }

    @OnClick(R.id.custom_header_iv_close)
    void closeView() {
        if (closeBtnCallback != null) {
            closeBtnCallback.closeResponse(ivClose);
        } else {
            ((Activity) getContext()).finish();
        }
    }

    @OnClick(R.id.custom_header_iv_right_btn)
    void clickRightBtn() {
        if (layoutClickCallback != null) {
            layoutClickCallback.rightBtn(ivRightBtn);
        }
    }

    /**
     * 右边点击 tv 按钮
     */
    @OnClick(R.id.custom_header_tv_right_btn)
    void tvRightClick() {
        if (layoutClickCallback != null) {
            layoutClickCallback.rightBtn(tvRightBtn);
        }
    }

    @BindView(R.id.rlTitle)
    RelativeLayout rlTitle;
    @BindView(R.id.custom_header_tv_title)
    TextView tvTitle;
    @BindView(R.id.custom_header_iv_back)
    ImageView ivBack;
    @BindView(R.id.custom_header_iv_close)
    ImageView ivClose;
    @BindView(R.id.custom_header_iv_right_btn)
    ImageView ivRightBtn;
    @BindView(R.id.custom_header_tv_right_btn)
    TextView tvRightBtn;
    private float titleSize;//标题字体大小
    private int imageResource;//图片资源
    private int titleBackground;
    private int titleTextColor;
    private String title;//标题
    private int backBtnWidth;//返回按钮宽
    private int backBtnHeight;//返回按钮高
    private int rightImgResId;
    private boolean openCloseBtn;
    private float rightTextSize;//右边按钮文字大小
    private String rightText;//右边按钮文本
    private Context context;


    public CommonHeaderView(@NonNull Context context) {
        this(context, null);
    }

    public CommonHeaderView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CommonHeaderView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CommonHeaderView, 0, 0);
        titleBackground = typedArray.getColor(R.styleable.CommonHeaderView_titleBackground, 0);
        titleTextColor = typedArray.getColor(R.styleable.CommonHeaderView_titleColor, 0);
        title = typedArray.getString(R.styleable.CommonHeaderView_commonTitle);
        titleSize = WindowUtils.px2dip(getContext(), typedArray.getDimension(R.styleable.CommonHeaderView_titleSize, 0));
        backBtnWidth = typedArray.getDimensionPixelSize(R.styleable.CommonHeaderView_backImgWidth, 0);
        backBtnHeight = typedArray.getDimensionPixelSize(R.styleable.CommonHeaderView_backImgHeight, 0);
        imageResource = typedArray.getResourceId(R.styleable.CommonHeaderView_backBtnImg, 0);
        rightImgResId = typedArray.getResourceId(R.styleable.CommonHeaderView_rightImgBtn, 0);
        openCloseBtn = typedArray.getBoolean(R.styleable.CommonHeaderView_openCloseBtn, false);
        rightTextSize = WindowUtils.px2dip(getContext(), typedArray.getDimensionPixelSize(R.styleable.CommonHeaderView_rightTvSize, 0));
        rightText = typedArray.getString(R.styleable.CommonHeaderView_rightTvText);
        typedArray.recycle();
        View view = LayoutInflater.from(context).inflate(R.layout.common_header_view, this, false);
        ButterKnife.bind(this, view);
        addView(view);
        initView();
    }

    private void initView() {
        setTitleColor(titleBackground);
        setTitle(title);
        setTitleTextColor(titleTextColor);
        if (imageResource != 0) {
            setIvBackImg(imageResource);
        }
        if (backBtnWidth != 0 && backBtnHeight != 0) {
            setBackBtnWH(backBtnWidth, backBtnHeight);
        }
        if (titleSize != 0) {
            setTitleSize(titleSize);
        }
        if (rightImgResId != 0) {
            setRightImgBtn(rightImgResId);
        }
        if (openCloseBtn) {
            ivClose.setVisibility(VISIBLE);
        }
        if (!TextUtils.isEmpty(rightText)) {
            setRightText(rightText);
        }
        if (rightTextSize != 0) {
            setRightTextSize(rightTextSize);
        }
    }

    /**
     * 设置打开或关闭左边 x掉按钮
     *
     * @param openCloseBtn 是否倒开x按钮
     * @return this
     */
    public CommonHeaderView setCloseBtnState(boolean openCloseBtn) {
        this.openCloseBtn = openCloseBtn;
        if (openCloseBtn) {
            ivClose.setVisibility(VISIBLE);
        } else {
            ivClose.setVisibility(GONE);
        }
        return this;
    }

    /***
     * 设置标题背景
     * @param titleColor
     * @return
     */
    public CommonHeaderView setTitleColor(int titleColor) {
        titleBackground = titleColor;
        rlTitle.setBackgroundColor(titleBackground);
        return this;
    }

    /**
     * 设置标题内容
     *
     * @param text 标题名字
     */
    public CommonHeaderView setTitle(String text) {
        title = text;
        tvTitle.setText(text);
        return this;
    }

    /**
     * 标题字体颜色
     *
     * @param textColor 字体颜色
     * @return this
     */
    public CommonHeaderView setTitleTextColor(int textColor) {
        tvTitle.setTextColor(textColor);
        return this;
    }

    /**
     * 标题字体大小
     *
     * @param textSize 文本大小
     * @return this
     */
    public CommonHeaderView setTitleSize(float textSize) {
        tvTitle.setTextSize(textSize);
        return this;
    }

    /**
     * 设置返回按钮图片
     *
     * @param resId 资源id
     */
    public CommonHeaderView setIvBackImg(int resId) {
        ivBack.setImageResource(resId);
        return this;
    }

    /***
     * 设置返回按钮显示隐藏的状态
     * @param visibility 状态
     */
    public CommonHeaderView setBackVisibility(int visibility) {
        ivBack.setVisibility(visibility);
        return this;
    }

    /**
     * 右边文本内容
     *
     * @param text 文本
     * @return this
     */
    public CommonHeaderView setRightText(String text) {
        tvRightBtn.setVisibility(VISIBLE);
        tvRightBtn.setText(text);
        return this;
    }

    /**
     * 设置右边文本按钮字体大小
     *
     * @param rightTextSizeDp textSize
     * @return this
     */
    public CommonHeaderView setRightTextSize(float rightTextSizeDp) {
        tvRightBtn.setVisibility(VISIBLE);
        tvRightBtn.setTextSize(rightTextSizeDp);
        return this;
    }

    /**
     * 设置右边图片资源
     *
     * @param resId 资源id
     * @return this
     */
    public CommonHeaderView setRightImgBtn(int resId) {
        ivRightBtn.setVisibility(VISIBLE);
        ivRightBtn.setImageResource(resId);
        return this;
    }

    /**
     * 设置返回按钮宽高
     *
     * @param width  宽度
     * @param height 高度
     */
    public CommonHeaderView setBackBtnWH(int width, int height) {
        int pW = WindowUtils.dip2px(context, 30);
        int pH = WindowUtils.dip2px(context, 30);
        //需要加上默认的padding
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width + pW, height + pH);
        ivBack.setLayoutParams(params);
        return this;
    }

    LayoutClickCallback layoutClickCallback;

    public CommonHeaderView setClickCallback(LayoutClickCallback layoutClickCallback) {
        this.layoutClickCallback = layoutClickCallback;
        return this;
    }

    BackClickCallback backClickCallback;

    public CommonHeaderView setBackCLickListener(BackClickCallback backClickCallback) {
        this.backClickCallback = backClickCallback;
        return this;
    }

    CloseCallback closeBtnCallback;

    public CommonHeaderView setCloseBtnCallback(CloseCallback closeBtnCallback) {
        this.closeBtnCallback = closeBtnCallback;
        return this;
    }

    /**
     * 点击回调
     */
    public interface LayoutClickCallback {
        /**
         * 右边按钮
         *
         * @param v view
         */
        void rightBtn(View v);
    }

    public interface BackClickCallback {
        void back(View v);
    }

    public interface CloseCallback {
        void closeResponse(View v);
    }

}
